import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
    public List<List<Integer>> fourSum(int[] nums, int target) {
        List<List<Integer>> lists = new ArrayList<>();
        Arrays.sort(nums);

        for (int j = 0; j < nums.length - 3; j++) {
            while (j != 0 && j < nums.length - 3 && nums[j] == nums[j - 1]) {
                j++;
            }
            for (int i = j + 1; i < nums.length - 2; i++) {
                while (i != j + 1 && i < nums.length - 2 && nums[i] == nums[i - 1]) {
                    i++;
                }
                int left = i + 1;
                int right = nums.length - 1;
                long target_tmp = (long) target - nums[j] - nums[i];
                while (left < right) {
                    if (nums[left] + nums[right] < target_tmp) {
                        left++;
                    } else if (nums[left] + nums[right] > target_tmp) {
                        right--;
                    } else {
                        List<Integer> list = new ArrayList<>();
                        list.add(nums[j]);
                        list.add(nums[i]);
                        list.add(nums[left]);
                        list.add(nums[right]);
                        lists.add(list);
                        left++;
                        right--;
                        while (left < right && nums[left] == nums[left - 1]) {
                            left++;
                        }
                        while (left < right && nums[right] == nums[right + 1]) {
                            right--;
                        }
                    }
                }
            }
        }
        return lists;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.fourSum(new int[]{1000000000, 1000000000, 1000000000, 1000000000}, -294967296));
    }
}